package com.aco.party.service.system;

import com.aco.party.dao.custom.MetaDao;
import com.aco.party.enums.MetaTypeEnum;
import com.aco.party.model.po.Meta;
import com.aco.party.model.vo.MetaParam;
import com.aco.party.model.vo.MetaVo;
import com.aco.party.model.vo.common.PageParam;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by flying on 3/10/2016 0010.
 */
@Service
public class MetaService {
    @Resource
    private MetaDao metaDao;

    public List<MetaVo> getByPage(PageParam pageParam,MetaParam param) {
        List<MetaVo> list = new ArrayList<>();
        List<Meta> metas = metaDao.getList(pageParam,param);
        MetaVo vo;
        if (metas != null) {
            for (Meta meta : metas) {
                vo = new MetaVo();
                BeanUtils.copyProperties(meta, vo);
                vo.setTypeStr(MetaTypeEnum.getMetaTypeEnum(vo.getType()).getDesc());
                list.add(vo);
            }
        }
        return list;
    }

    public Integer getTotal(MetaParam param) {
        return metaDao.getTotal(param);
    }

    public void update(Meta meta) {
        meta.setUpdateTime(new Date());
        metaDao.updateByPrimaryKeySelective(meta);
    }

    public void add(Meta meta) {
        meta.setCreateTime(new Date());
        metaDao.insertSelective(meta);
    }

    public void delete(Long id) {
        metaDao.deleteByPrimaryKey(id);
    }

    public Meta getByPrimaryKey(Long id) {
        return metaDao.selectByPrimaryKey(id);
    }

    public List<Meta> getByType(String type){return metaDao.getByType(type);}

    public Meta getByName(String type){return metaDao.getByName(type);}
}
